/**
 * http相关
 */

import axios from 'axios'
import qs from "qs"
import router from '../router'
import config from '../config'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// axios配置
axios.defaults.timeout = 30000
axios.defaults.baseURL = config.host
// 允许跨域请求
// axios.defaults.withCredentials = true

axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.transformRequest = [ data => {
  let ret = ''
  for (let it in data) {
    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  }
  return ret
}]

axios.interceptors.request.use(config => {
  NProgress.start()
  // config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})


// http response 拦截器
axios.interceptors.response.use(
  response => {
    NProgress.done()
    //去判断
    // if (response.data.status === 401) {
    //   this.$router.push('/login');
    // }
    return response.data
  },
  error => {
    NProgress.done()
    return Promise.reject(error)
  }
)

export default axios
